package aes.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import aes.general.StudentControl;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;


/**
 * This panel allows the students to view their finished 
 * and already approved exams. The students are also can 
 * get a copy of thier exams.  
 */
public class StudentViewExamPanel extends JPanel {
/**
 * 
 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JLabel l1;
	private JButton btnBack;
	private JButton btnDownload;
	private JSeparator separator;
	private TextArea examtext;
	private Vector<Vector<String>> info ;
	
	public StudentViewExamPanel(JPanel jp) {
		super();
		this.jp = jp;
		initialize();
	}

	private void initialize() {
		setLayout(null);

		l1 = new JLabel("View Exam");
		l1.setFont(new java.awt.Font("Tempus Sans ITC", java.awt.Font.BOLD, 21));
		l1.setBounds(267, 5, 106, 29);
		add(l1);
		
		separator = new JSeparator();
		separator.setBounds(10, 38, 620, 2);
		add(separator);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				StudentControl.setFinishedExamInfo(null);
				CardLayout cl = (CardLayout)(jp.getLayout());
				 cl.show(jp,"MainPanel");	
			}
		});
		btnBack.setBounds(197, 610, 100, 23);
		add(btnBack);
		
		examtext = new TextArea();
		examtext.setBackground(new Color(230, 230, 250));
		examtext.setEditable(false);
		examtext.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 13));
		examtext.setBounds(20, 46, 586, 550);
		add(examtext);
		
		btnDownload = new JButton("Download");
		btnDownload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FileDialog fileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);

				fileDialog.setFilenameFilter(new FilenameFilter() {
				    public boolean accept(File dir, String name) {
				        return name.endsWith(".doc");
				    }
				});
				fileDialog.setFile("Exam");
				fileDialog.setVisible(true);
				if (fileDialog.getFile()==null) return;  //user clicked cancel in filedialog window

				Document doc = new Document();

				try {
					RtfWriter2.getInstance(doc, new FileOutputStream(fileDialog.getDirectory()+fileDialog.getFile()+".doc"));
					doc.open();
					
					Paragraph p = new Paragraph(examtext.getText());
					doc.add(p);
					doc.close();

				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (DocumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		btnDownload.setBounds(330, 610, 100, 23);
		add(btnDownload);
		
		while((info = StudentControl.getFinishedExamInfo()) == null);
		int j=1;
		for(int i=0; i<info.size(); i++){
			examtext.append("\n");	
			examtext.append("Question no : "+ j++ + "\n");	
			examtext.append(info.get(i).get(1)+ "\n");
			examtext.append("\n");
			examtext.append(" 1) "+ info.get(i).get(2)+ "\n ");
			examtext.append("2) "+ info.get(i).get(3)+ "\n ");
			examtext.append("3) "+ info.get(i).get(4)+ "\n ");
			examtext.append("4) "+ info.get(i).get(5)+ "\n ");
			examtext.append("correct answer is: "+ info.get(i).get(6) + "\n ");
			examtext.append("your answer is: "+ info.get(i).get(7)+ "\n ");
//			examtext.append("\n");
		}
	//	examtext.append("Your Grade Is: "+ info.get(1).get(8)+ "\n ");
	}
}
